package com.jnu.myAccount.menu.home;

import static com.jnu.myAccount.MyTextInputWatcher.getStandardDateString;
import static com.jnu.myAccount.MyTextInputWatcher.getStandardFloatString;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jnu.myAccount.MyTextInputWatcher;
import com.jnu.myAccount.record.DataPackage;
import com.jnu.myAccount.record.FundAdapter;
import com.jnu.myAccount.record.Record;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeViewModel extends ViewModel {
    private Map<String, List<Record>> recordMap = new HashMap<>();
    private DataPackage dataPackage = DataPackage.Instance();
    private MutableLiveData<Float> budget = new MutableLiveData<>();
    private MutableLiveData<Float> balance = new MutableLiveData<>();
    private MutableLiveData<String> inText=new MutableLiveData<>();
    private MutableLiveData<String> outText=new MutableLiveData<>();
    private String year;
    private String month;
    private float in;
    private float out;

    //删除记录并更新数据
    public void deleteRecord(String date,int index) {
        Record recordToDelete = recordMap.get(date).get(index);
        dataPackage.removeRecord(recordToDelete);
        syncInDateInfo();
        balance.postValue(dataPackage.getBalance());
        getDetail();
    }

    //同步map中的数据
    public void syncInDateInfo()
    {
        if(recordMap == null)
            recordMap = new HashMap<>();
        recordMap.clear();
        Map<String, List<Record>> map = dataPackage.getDateFundList();

        if(map == null)
            return;
        recordMap.putAll(map);
    }

    public void loadData() {
        syncInDateInfo();
        budget.postValue(dataPackage.getBalance());
        getDateRangeString();
        getDetail();
    }

    //更新本月收支的值和文本
    public void getDetail() {
        in = out = 0;
        String regex = year+"-"+month+"-[0-9]+";
        Map<String, List<Record>> map = recordMap;
        if(map==null)
            return;
        for(String date : map.keySet()) {
            if(date.matches(regex)) {
                for(Record info : map.get(date)) {
                    if(info.isInOrOutValue()) {
                        in += info.getFundValue();
                    }else {
                        out += info.getFundValue();
                    }
                }
            }
        }
        inText.postValue(getStandardFloatString(in));
        outText.postValue(getStandardFloatString(out));
    }

    public MutableLiveData<Float> getBudget() {
        return budget;
    }

    public String getBalanceString()
    {
        return String.valueOf(budget.getValue());
    }

    public void setBudget(float budget)
    {
        this.budget.postValue(budget);
    }

    //获取本月起始至结束的字符串
    public String getDateRangeString()
    {
        Date date = new Date(System.currentTimeMillis());
        String[] strings = getStandardDateString(date).split("-");
        year = strings[0];
        month = strings[1];
        String[] dateRange = MyTextInputWatcher.getDateRange(Integer.parseInt(year)
                ,Integer.parseInt(month));
        return dateRange[0]+"至"+dateRange[1];
    }

    public void setBalance(String balance)
    {
        if (!balance.equals(""))
        {
            dataPackage.setBalance(Float.parseFloat(balance));
            this.budget.postValue(Float.parseFloat(balance));
        }
    }

    public void Refreshing()
    {
        budget.postValue(dataPackage.getBalance());
        getDateRangeString();
        getDetail();
    }

    public float calNetAsset(){
        float netAsset = 0;
        Map<String, List<Record>> dateRecordMap = dataPackage.getDateFundList();
        List<String> dateList = dataPackage.getDateList();
        for (String date:dateList){
            for(Record info : dateRecordMap.get(date)) {
                if (info.isInOrOutValue()){
                    netAsset+=info.getFundValue();
                }else {
                    netAsset-=info.getFundValue();
                }
            }
        }
        return netAsset;
    }

    public String getBudgetBalanceString()
    {
        if(budget.getValue()==0)
        {
            return getStandardFloatString(calNetAsset());
        }else {
            return getStandardFloatString(budget.getValue()-out);
        }
    }

    public String getBudgetBalanceTitleString()
    {
        if(budget.getValue()==0)
            return "净资产";
        else
            return "本月预算余额";
    }

    public String getDate(int index)
    {
        return dataPackage.getDateList().get(index);
    }

    public Map<String, List<Record>> getRecordMap() {
        return recordMap;
    }


    public String getTotalIn(String date) {
        float sum=0;
        List<Record> list = recordMap.get(date);
        for(Record info:list)
        {
            if(info.isInOrOutValue())
                sum+=info.getFundValue();
        }
        return  getStandardFloatString(sum);
    }

    public String getTotalOut(String date) {
        float sum=0;
        List<Record> list = recordMap.get(date);
        for(Record info:list)
        {
            if(!info.isInOrOutValue())
                sum+=info.getFundValue();
        }
        return  getStandardFloatString(sum);
    }

    public MutableLiveData<String> getInText() {
        return inText;
    }

    public MutableLiveData<String> getOutText() {
        return outText;
    }

}
